package cn.tedu.logistics.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.logistics.bean.Goods;
import cn.tedu.logistics.bean.GoodsType;
import cn.tedu.logistics.service.GoodsService;

@Controller
@RequestMapping("goods")
public class GoodsController {

	//商品业务管理
	@Autowired
	private GoodsService goodsService;
	
	//商品信息页面
	@RequestMapping("toGoodsInfo")
	public String toGoodsInfo(Model model) {
		List<Goods> list = new ArrayList<Goods>();
		list = goodsService.findGoodsInfo();
		model.addAttribute("goodsInfo", list);
		return "goodsInfo";
	}
	
	//添加商品信息页面
	@RequestMapping("toAddGoodsInfo")
	public String toAddGoodsInfo(Model model) {
		List<GoodsType> list = new ArrayList<GoodsType>();
		list = goodsService.findGoodsType();
		model.addAttribute("goodsType", list);
		return "addGoodsInfo";
	}
	
	//添加商品信息
	@RequestMapping("addGoodsInfo")
	public String addGoodsInfo(Goods goods) {
		goodsService.addGoodsInfo(goods);
		return "redirect:toGoodsInfo";
	}
	
	//删除商品信息
	@RequestMapping("delGoodsInfo")
	public String delGoodsInfo(Integer id) {
		goodsService.delGoodsInfoById(id);
		return "redirect:toGoodsInfo";
	}
	
	//更新商品信息页面
	@RequestMapping("toUpdGoodsInfo")
	public String toUpdGoodsInfo(Integer id,Model model) {
		List<GoodsType> list = new ArrayList<GoodsType>();
		list = goodsService.findGoodsType();
		model.addAttribute("goodsType", list);
		Goods goods = goodsService.findGoodsById(id);
		model.addAttribute("goods", goods);
		return "updGoodsInfo";
	}
	
	//更新商品信息
	@RequestMapping("updGoodsInfo")
	public String updGoodsInfo(Goods goods) {
		goodsService.updGoods(goods);
		return "redirect:toGoodsInfo";
	}
	
	//商品类型页面
	@RequestMapping("toGoodsType")
	public String toGoodsType(Model model) {
		List<GoodsType> list = new ArrayList<GoodsType>();
		list = goodsService.findGoodsType();
		model.addAttribute("goodsType", list);
		return "goodsType";
	}
	//添加商品类型页面
	@RequestMapping("toAddGoodsType")
	public String toAddGoodsType() {
		return "addGoodsType";
	}
	//添加商品类型
	@RequestMapping("addGoodsType")
	public String addGoodsType(GoodsType goodsType) {
		goodsService.addGoodsType(goodsType);
		return "redirect:toGoodsType";
	}
	
	//删除商品类型
	@RequestMapping("delGoodsType")
	public String delGoodsType(Integer id) {
		List<String> list = goodsService.findGoodsIdByTypeId(id);
		if(list!=null && list.size()!=0) {
			goodsService.delGoodsInfo(list);
		}
			goodsService.delGoodsTypeById(id);
		return "redirect:toGoodsType";
	}
	
	//更新商品类型页面
	@RequestMapping("toUpdGoodsType")
	public String toUpdGoodsType(Integer id,Model model) {
		GoodsType goodsType = goodsService.findGoodsTypeById(id);
		model.addAttribute("goodsType", goodsType);
		return "updGoodsType";
	}
	//更新商品类型
	@RequestMapping("updGoodsType")
	public String updGoodsType(GoodsType goodsType) {
		goodsService.updGoodsType(goodsType);
		return "redirect:toGoodsType";
	}
	
	
}
